﻿//加载广告面板
function btnGuangGao() {
    showTool();
    var tools = getTools();

    var html = "";
    html += "<div class='d_z adtitle clearfix'><div class='adfl dw45'>插入广告(点击插入)</div><div class='adfr adfwn dw45 adtar'><span class='d_z adbtn adbgcred' data-f='guanggaoxiugai()'>添加广告</span><span class='d_z adbtn' data-f='webzifanhui()'>返回</span></div></div><div class='d_z adimages adtac admt5 clearfix'></div><div class='adnexturl' >向下滑动加载更多...</div>";

    tools.html(html);
    loadGuangGao(1);
    bindButtonClickEvent();
}

//广告确定
function guanggaoqueding(text, img, link) {
    hideMenu();

    var img = img && "<img class='adimg100' src='" + img + "'/>";
    //插入图像的大小
    var span = $("<div style='display: block;text-align: center;font-size: 18px;'><a href='javascript:tolxwurl(\"/tourl?link=" + link + "\");'>" + img + "</br>" + G_TUPIANELEMENT.html() + "</a></div>");
    //插入内容
    G_CLICKELEMENT.before(span);
    bindElementClickEvent(span, elementClickEvent);
}

//显示编辑广告
function guanggaoxiugai(link) {
    var title = link ? "编辑广告" : "添加广告";
    link = link || "";

    showTool();
    var tools = getTools();

    var html = "";
    html += "<div class='d_z adtitle clearfix'><div class='adfl dw45'>" + title + "</div><div class='adfr adfwn dw50 adtar'><span class='d_z adbtn adbgcred adguanggaosave' data-f='saveguanggaoxiugai(" + link + ")'>保存</span><span class='d_z adbtn' data-f='btnGuangGao()'>返回</span></div></div>";

    html += "<div>";
    html += "广告文字<br/>";
    html += "<textarea class='d_z adtextarea  adguanggaotext adf14' placeholder='广告文字：请长按此粘贴!'></textarea></br>";
    //html += "<span class='d_z adbtn'>清空</span></br>";
    html += "链接地址，点击广告跳转到此地址<br/>";
    html += "<textarea class='d_z adtextarea adguanggaolink' placeholder='链接地址：请长按此粘贴!'></textarea></br>";
    //html += "<span class='d_z adbtn'>清空</span></br>";
    html += "广告图片<br/>";
    html += "<div class='adw100 adguanggaoimg'></div>";
    html += "<span class='d_z adbtn file'>上传图片<input type=\"file\" /></span></br>";
    html += "</div>";

    tools.html(html);
    link && loadguangaolink(link);
    bindButtonClickEvent();
}

//加载广告
function loadguangaolink(id) {
    $.get("/loadguangaolink?id=" + id, function (ret) {
        $(".adguanggaotext").val(ret.text);
        $(".adguanggaolink").val(ret.link);
        $(".adguanggaoimg").html("<img class='adimg100' src='" + ret.img+ "'/>");
    },"json");
}

//广告上传完毕
function addGuangGaoIMG(url) {
    $(".adguanggaoimg").html("<img class='adimg100' src='"+url+"'/>");
}

//保存广告
function saveguanggaoxiugai(id) {
    if ($(".adguanggaotext").val() == "") {
        alert("请维护广告文字");
        return;
    }

    //保存广告
    $.post("/saveguanggaoxiugai", {
        text: $(".adguanggaotext").val(),
        link: $(".adguanggaolink").val(),
        img: $(".adimg100").length > 0 ? $(".adimg100").attr("src") : "",
        guanggaoid:id||""
    }, function (ret) {
        //$(".adguanggaosave").attr("data-f", "saveguanggaoxiugai(" + ret + ")");

        //跳转到广告界面
        btnGuangGao();
    });
}

//插入广告到容器里面 这里可能会出现问题
function addGuangGaoPan(text, img, link) {
    showlog(text);
    getAdImages().append('<div class="adbtnX  adtal adb1gray adm1 adbr4 adw80 ad_gg admt1" style="min-height:50px;border: 1px dotted gray" data-f="guanggaoshowbtn(event.target,\'' + escape(text) + '\',\'' +escape( img) + '\',\'' + link + '\')"">' + text + '</div>');


    $(".ad_gg").width( $(".adimages").width() - 4 );
}

function loadGuangGao(page) {
    getNextUrl().html("正在加载中...");

    $.post("/getguanggao", { page1: page }, function (ret) {
        getNextUrl().remove();
        if (ret.length > 0) {
            for (var o = 0; o< ret.length;o++) {
                addGuangGaoPan(ret[o].text, ret[o].img, ret[o].link);
            }
            if (ret.length == 10)
                getAdImages().after("<div class='adnexturl' data-f='loadGuangGao(" + (page + 1) + ")' >向下滑动加载更多广告...</div>");
        }
        else {
            if (page == 1)
                getAdImages().html("您还没有广告!");

            getNextUrl()&&getNextUrl().html("");
        }

        bindButtonClickEvent();
    },"json");
}

function guanggaoshowbtn(ths, text,img,link) {
    if (getTuPianBtn().length > 0)
        getTuPianBtn().remove();

    var div = $("<div class='adfwn adtar adtupianshowbtn'><span class='d_z adbtn adbgcred' data-f='guanggaoqueding(\"" + escape(text) + "\",\"" + escape(img) + "\",\"" + escape(link) + "\")'>选择</span><span class='d_z adbtn' data-f='guanggaoxiugai(\"" + link + "\")'>修改</span><span class='d_z adbtn' data-f='guanggaoshanchu(\"" + link + "\")'>删除</span></div></div>");

    getAdImages().append(div);

    G_TUPIANELEMENT = $(ths);
    var x = G_TUPIANELEMENT.position().left;
    var y = G_TUPIANELEMENT.position().top;
    var w = G_TUPIANELEMENT.width();
    var h = G_TUPIANELEMENT.height();

    $(".adb1red").removeClass("adb1red");
    G_TUPIANELEMENT.addClass("adb1red");

    getTuPianBtn().css({ left: x + (w - $(".adtupianshowbtn").outerWidth(true))/2, top: y+(h - $(".adtupianshowbtn").outerHeight(true))/2 });
    getTuPianBtn().show();

    bindButtonClickEvent();
}

function guanggaoshanchu(link) {
    if (getTuPianBtn().length > 0)
        getTuPianBtn().remove();

    if (G_TUPIANELEMENT && G_TUPIANELEMENT.length > 0)
        G_TUPIANELEMENT.remove();

    $.get("/deleteguanggao?purl=" + link, function (ret) {

    });
}
